// 引入腾讯地图jssdk文件
import  QQMapWX from "@/common/js/qqmap-wx-jssdk.min.js"
module.exports = {
	methods: {
		onLocation(fn) {
			let _this = this
			// 获取用户授权信息
			wx.getSetting({
				success(res) {
					// console.log(res, res.authSetting['scope.userLocation'], 'location')
					// if (!res.authSetting['scope.userLocation']) {
						let authLocation = res.authSetting['scope.userLocation']
						let status = -1
						if(authLocation == undefined) {
							status = -1
						} else if(authLocation) {
							status = 1
						} else {
							status = 0
						}
						// console.log(status, 'false')
						_this.$store.commit('setLocation', status);
					// }
					fn && fn()
				}
			})
		},
		// 获取权限
		getAuthLocation(fn) {
			let _this = this
			let authLocation = uni.getStorageSync('authLocation')
			if (authLocation == -1) {
				// 提示授权
				wx.authorize({
					scope: 'scope.userLocation',
					success () {
						_this.$store.commit('setLocation', 1);
						fn && fn()
					},
					fail() {
						console.log('fail')
						_this.$store.commit('setLocation', 0);
					}
				})
			} else {
				wx.openSetting({
					success: function (res) {
						if (res.authSetting["scope.userLocation"] == true) {
							_this.$store.commit('setLocation', 1);
							wx.showToast({
								title: '授权成功',
								icon: 'success',
								duration: 1000
							})
							//再次授权，调用wx.getLocation的API
							fn && fn()
						} else {
							wx.showToast({
								title: '授权失败',
								icon: 'none',
								duration: 1000
							})
						}
					},
				})
			}
		},
		geo(fn) {
			wx.getLocation({
				type: 'gcj02',
				success: (res) => {
					if(fn) {
						fn(res)
					} else {
						return res
					}
				}
			});
		},
		getChoosePoi(fn) {
			console.log(true, 'getchoose')
			// wx.onLocationChange({
			// 	type: 'wgs84',
			// 	success: (res) => {
			// 		console.log(res)
			// 	}
			// })
			const _locationChangeFn = function(res) {
			  console.log('location change', res)
			 }
			 wx.onLocationChange(_locationChangeFn)
			 wx.offLocationChange(_locationChangeFn)
		},
		getAddressInfo(res, fn) {
			let _this = this
			// 实例化API核心类
			let qqmapsdk = new QQMapWX({
				// 填写自己的Key值，这个值是与AppID绑定的
				key: 'HCEBZ-WNEK3-V4E35-YWTKL-SDCU5-3LFS6'
			});
			// 逆地址解析方法
			qqmapsdk.reverseGeocoder({
				location: {
					latitude: res.latitude,
					longitude: res.longitude
				},
				success(res) {
					var newCity = {}
					// 取到用户的定位城市，赋值传递出去
					newCity.city = res.result.address_component.city
					newCity.district = res.result.address_component.district
					newCity.street = res.result.address_component.street
					newCity.province = res.result.address_component.province
					newCity.address = res.result.address
					newCity.name = res.result.ad_info.name
					newCity.city_code = res.result.ad_info.city_code
					newCity.lat = res.result.location.lat
					newCity.lng = res.result.location.lng
					_this.$store.commit('newCityFun', newCity)
					fn && fn()
				},
				fail(res) {
					console.log("逆地址解析失败")
					console.log(res)
				}
			})
		}
	}
}